<?php

/**
 * 获取该房间当天之前已预订或已入住的房间信息
 * @modify wenhui 2016-11-21 创建
 * @param  integer   $hotelId   客栈ID
 * @param  int|array   $roomId    房间ID
 * @param  int   $orderRoomId    orderRoom表的ID
 * @return mixed   $data      房间数据
 */
function ValidateCheckIn($hotelId, $roomId ,$orderRoomId = '', $today = null) {
    // 获取已预订或已入住 子订单关联房间表数据
    // 0=已预订 2=已入住
    $orderRoomModel = D('OrderRoom');
    $statusList = '0, 1, 2';
    $orderRoomInfos = $orderRoomModel->getInfoByHotelRoom($hotelId, $roomId, $statusList);

    if (!is_null($today)) {
        $todayOrderRoomInfo = $orderRoomInfos;
        foreach ($todayOrderRoomInfo as $key => $todayOrderRoomItme) {
            if (date('Y-m-d',strtotime($todayOrderRoomItme['enddate'])) < date('Y-m-d',strtotime($today))) {
                unset($todayOrderRoomInfo[$key]);
            }
        }
        unset($todayOrderRoomItme);

        return $todayOrderRoomInfo;
    }

    if (!empty($orderRoomId)) {
        // 获取当前订单的开始时间
        $orderRoom = $orderRoomModel->getInfo($orderRoomId);
        $orderStartTime = date('ymd', strtotime($orderRoom[0]['startdate']));
    }

    // 获取客栈脏房数据
    $roomInfoModel = D('RoomInfo');
    $roomInfos = $roomInfoModel->getInfo($roomId);
    $data = array(
        'orderRoomInfos' => $orderRoomInfos,
        'roomInfos' => $roomInfos,
    );

    // 处理数据
    $resultData = array();
    foreach ($data as $key => &$orderRoomData) {
        if (!empty($orderRoomData) && $key == 'orderRoomInfos') {
            foreach ($orderRoomData as &$roomData) {

                // 办理入住时 该房间该订单已预订 所以过滤掉该房当天已预订的订单
                if (!empty($orderRoomId)) {

                    if (date('ymd', strtotime($roomData['startdate'])) < $orderStartTime) {
                        $orderRoom = array(
                            'roomId' => $roomData['roominfo_id'],
                            'roomName' => $roomData['roominfo_name'],
                            'status' => $roomData['status'],
                        );
                        $resultData['orderRoomInfos'][] = $orderRoom;
                    }
                } else {

                    if (date('ymd', strtotime($roomData['startdate'])) < date('ymd', time())) {
                        $orderRoom = array(
                            'roomId' => $roomData['roominfo_id'],
                            'roomName' => $roomData['roominfo_name'],
                            'status' => $roomData['status'],
                        );
                        $resultData['orderRoomInfos'][] = $orderRoom;
                    }
                }
            }
            unset($roomData);
        }
        if (!empty($orderRoomData) && $key == 'roomInfos') {

            // 处理$key=‘roomInfos’数组  根据 isset($orderRoomData['hotelentity_id']) 区分二维数组，一维数组
            if (!isset($orderRoomData['hotelentity_id'])) {
                foreach ($orderRoomData as &$roomItem) {

                    if ($roomItem['isdirty'] == 1) {
                        $orderInfo = array(
                            'roomId' => $roomItem['id'],
                            'isdirty' => $roomItem['isdirty'],
                            'roomName' => $roomItem['name'],
                        );
                        $resultData['roomInfos'][] = $orderInfo;
                    }
                }
                unset($roomItem);
            } else {

                if ($orderRoomData['isdirty'] == 1) {
                    $orderInfo = array(
                        'roomId' => $orderRoomData['id'],
                        'isdirty' => $orderRoomData['isdirty'],
                        'roomName' => $orderRoomData['name'],
                    );
                    $resultData['roomInfos'][] = $orderInfo;
                }
            }
        }
    }
    unset($orderRoomData);

    // 根据房间id获取最新的房间名称 并组装数据
    $orderLogic = A('Common/Order', 'Logic');
    $roomInfo = $orderLogic->getRoomInfo($roomId);

    foreach ($resultData as &$dirtyDataItem) {
        foreach ($dirtyDataItem as &$dirtyDataItemList) {
            foreach ($roomInfo as &$roomItem) {
                if ($roomItem['id'] === $dirtyDataItemList['roomId']) {
                    $dirtyDataItemList['roomName'] = $roomItem['name'];
                }
            }
            unset($roomItem);
        }
        unset($dirtyDataItemList);
    }
    unset($dirtyDataItem);
 
    return $resultData;
}


/**
 * 获取客栈房型最低价、最高价、客栈房间数（默认不获取）
 * @modify wenhui 2017-02-21 创建
 * @param  integer   $hotelId   客栈ID
 * @param  bool   $hotelRoom    是否获取房间数
 * @return mixed   $data      房间数据
 */
function getHoteRoomTypePrice($hotelId, $hotelRoom = false) {
    $hotelRoomTypePriceModel = D('HotelRoomTypePrice');
    $roomTypePriceModel = D('RoomTypePrice');
    $roomTypeDetailModel = D('RoomTypeDetail');
    $roomTypeModel = D('RoomType');
    $roomInfoModel = D('RoomInfo');
    // 根据客栈id获取所有房型id 获取客栈所有上架房型
    $roomTypeDetail = $roomTypeDetailModel->getInfoByHotel($hotelId, true);
    $roomTypeId = array_column($roomTypeDetail, 'roomtype_id');
    // 根据客栈id 获取每个客栈房间总数
    if ($hotelRoom) {
        if (!empty($roomTypeDetail)) {
           $roomInfo = $roomInfoModel->getInfoByRoomType($hotelId, $roomTypeId);
           $data['roomTotal'] = count($roomInfo);
        } else {
            $data['roomTotal'] = 0;
        }  
    }

    // 获取客栈房型当天价格日历的价格
    if (!empty($roomTypeDetail)) {
        foreach ($roomTypeDetail as $index => &$roomTypeDetailItem) {
            // 房型折扣值
            $roomTypeDiscountInfo = $roomTypeModel->getInfo($roomTypeDetailItem['roomtype_id']);
            // 过滤已被删除的房型
            if (empty($roomTypeDiscountInfo)) {
                unset($roomTypeDetail[$index]);
                continue;
            }
            $discount = ($roomTypeDiscountInfo['discount'])/10;

            // 获取预定价格
            $hotelRoomTypePriceList = $hotelRoomTypePriceModel->getBookPrice($roomTypeDetailItem['roomtype_id'], $startDate);
            $roomTypePriceList = $roomTypePriceModel->getBookPrice($roomTypeDetailItem['roomtype_id'], $startDate);
            if (!empty($hotelRoomTypePriceList)) {
                $roomTypeDetailItem['price'] = number_format($hotelRoomTypePriceList[0]['price'],2, '.', '');
            } else {

                if (!empty($roomTypePriceList)) {
                    $roomTypeDetailItem['price'] = number_format((($roomTypePriceList[0]['price']*1000)/1000) * $discount ,2, '.', '');
                } else {
                    $roomTypeDetailItem['price'] = number_format((($roomTypeDiscountInfo['price']*1000)/1000) * $discount, 2, '.', '');
                }
            }
        }
    }

    if (!empty($roomTypeDetail)) {
        // 获取客栈房型的最高价与最低价
        $roomTypeDetail = my_sort($roomTypeDetail, 'price', SORT_DESC, SORT_NUMERIC);
        $data['maxPrice'] = $roomTypeDetail[0]['price'];
        $data['minPrice'] = $roomTypeDetail[count($roomTypeDetail) - 1]['price'];
    }

    return isset($data) ? $data : array();
}
